﻿
Imports System.IO
Imports std
Imports storoomc
Imports storcore

Public Class slices_hash
    Inherits storoomOper_hash

    Private diskslices As diskslices = Nothing

    Protected Overrides Sub setSlices()
        diskslices = New diskslices(configNaming.filter.value.slicestype.slices_hash)
        openImpl(slices)
    End Sub

    Private Function showsliceImpl(ByVal slice As slicebase) As String
        Return slice.filename()
    End Function

    Public Overrides Function showslice(ByVal slice As storageInterface) As String
        Return showsliceImpl(convertor.cast(Of slicebase)(slice))
    End Function

    Protected Overrides Function modifyImpl(ByVal slice As storageInterface, _
                                            ByVal key As String, _
                                            ByVal buff As bytes) As Boolean
        If diskslices.sliceModifiable() Then
            Return MyBase.modifyImpl(slice, key, buff)
        Else
            slice.delete(key)
            Return slice.write(key, buff)
        End If
    End Function

    Protected Overrides Function fullImplOverride() As ternary
        If diskslices.capacity_full() Then
            Return ternary.true
        Else
            Return ternary.unknown
        End If
    End Function

    Protected Overrides Function capacityImplOverride(ByRef cap As UInt64, _
                                                      ByVal slices As vector(Of storageInterface)) As ternary
        If diskslices.capacity(cap) Then
            Return ternary.true
        Else
            Return ternary.false
        End If
    End Function

    Protected Overrides Function openImpl(ByVal slices As vector(Of storageInterface)) As Boolean
        'let slices.size() always less then upper bound
        Dim files(diskslices.maxSliceCount() - 2) As String
        For i As Int64 = 0 To diskslices.maxSliceCount() - 2
            files(i) = Convert.ToString(i)
        Next
        Return diskslices.open(slices, files, True)
    End Function
End Class
